<template>
  <!-- 如无需展开查询，请移除:advanced.sync="advanced"并添加:no-advanced="true" -->
  <e-pro-table title="测试" :advanced.sync="advanced">
    <template slot="query">
      <a-col :xxl="6" :xl="8" :lg="12" :sm="24">
        <a-form-model-item label="NO">
          <a-input-number v-model="queryParam.userId"/>
        </a-form-model-item>
      </a-col>
      <a-col :xxl="6" :xl="8" :lg="12" :sm="24">
        <a-form-model-item label="用户名">
          <e-dict-select type="test" v-model="queryParam.userName" @change="$refs.eTable.refresh(true)"/>
        </a-form-model-item>
      </a-col>
      <a-col :xxl="6" :xl="8" :lg="12" :sm="24">
        <a-form-model-item label="密码">
          <a-input v-model="queryParam.userPassword"/>
        </a-form-model-item>
      </a-col>
      <template v-if="advanced">
        <!-- 点击展开显示的查询条件 -->
      </template>
    </template>

    <template slot="button">
      <e-btn-add permissions="girls:info:save" to="/girls/info/input"/>
      <e-btn-remove-batch permissions="girls:info:remove" :loading.sync="removeBathLoading" :ids="selectedRowKeys" :click-callback="remove"/>
      <e-btn-import import-code="girls:info"/>
      <a-button icon="download" @click="exportData">导出数据</a-button>
    </template>

    <template slot="table">
      <s-table
        ref="eTable"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowSelection="rowSelection"
        showPagination="auto"
      >
        <span slot="userName" slot-scope="text">
          <e-dict-tag type="test" :code="text"/>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <e-btn-edit permissions="girls:info:save" :to="`/girls/info/input`" :tab-name="record.key" :id="record.id"/>
            <e-btn-remove permissions="girls:info:remove" :id="record.id" :divider="false" :click-callback="remove"/>
          </template>
        </span>
      </s-table>
    </template>
  </e-pro-table>
</template>

<script>
import { select, remove, exportData } from '@/api/girls/info'
import { downloadFileById } from '@/utils/util'
import { STable, Ellipsis } from '@/components'
import EProTable from '@/components/Easy/data-display/ProTable'
import EBtnAdd from '@/components/Easy/general/BtnAdd'
import EBtnEdit from '@/components/Easy/general/BtnEdit'
import EBtnRemove from '@/components/Easy/general/BtnRemove'
import EBtnRemoveBatch from '@/components/Easy/general/BtnRemoveBatch'
import EDictTag from '@/components/Easy/data-display/DictTag'
import EDictSelect from '@/components/Easy/data-entry/DictSelect'
import EBtnImport from '@/components/Easy/general/BtnImport'

const columns = [
  {
    title: 'NO',
    dataIndex: 'userId',
    width: 100,
    sorter: true
  },
  {
    title: '用户名',
    dataIndex: 'userName',
    width: 100,
    sorter: true,
    scopedSlots: { customRender: 'userName' }
  },
  {
    title: '密码',
    dataIndex: 'userPassword',
    width: 100,
    sorter: true
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: 90,
    fixed: 'right',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'GirlsInfoList',
  components: {
    STable,
    Ellipsis,
    EProTable,
    EBtnAdd,
    EBtnEdit,
    EBtnRemove,
    EBtnRemoveBatch,
    EDictTag,
    EDictSelect,
    EBtnImport
  },
  data () {
    this.columns = columns
    return {

      // 查询参数
      advanced: false,
      queryParam: {},
      selectedRowKeys: [],
      selectedRows: [],
      removeBathLoading: false
    }
  },
  activated () {
    this.$refs.eTable.refresh(true)
  },
  computed: {
    rowSelection () {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    // 加载数据方法 必须为 Promise 对象
    loadData (parameter) {
      const requestParameters = Object.assign({}, parameter, this.queryParam)
      return select(requestParameters)
        .then(res => {
          return res.data
        })
    },
    exportData () {
      exportData(this.queryParam).then(res => {
        downloadFileById(res.data)
      })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    remove (id) {
      remove(id).then(res => {
        this.$refs.eTable.refresh(true)
        this.removeBathLoading = false
      }).catch(({ response }) => {
        this.removeBathLoading = false
      })
    }
  }
}
</script>
